For...Next Statement

Executes a series of statements a specified number of times. The For Each...Next statement is a variation that executes once for each element of a one-dimensional array.


Syntax

For loops have two syntaxes:

For counter [AS datatype] = start To | DownTo end [Step value]

[Statements]

[ Continue]

[ Exit]

[Statements]

Next [counter]

PartDescription
counter A local variable that will be incremented each time the loop executes. It can be an Integer, Single, or Double.
datatype Optional: Datatype of the counter. It will be Integer, Single, or Double. If you use the optional AS clause, you do not have to declare the counter variable with a Dim statement. When you use the AS clause instead of a Dim statement, the counter variable goes out of scope at the end of the For statement rather than at the end of the method or function.
start Initial value of counter.
end Final value of counter.
value Amount to increment or decrement counter each time statements are executed. Value is 1 unless otherwise specified. "To" indicates increment; "DownTo" decrement.
statements Statements to be executed the specified number of times (until counter is greater than end, or less than end if DownTo is used).
Continue If a Continue statement is present, execution skips over the remaining statements in the For loop and resumes with a new iteration of the loop. Optional parameters of the Continue statement allow you to specify which loop will iterate next, in the case of nested loops.
Exit If an Exit statement is present, execution skips over the remaining statements in the loop and resumes with the statement following the Next statement.


Notes

The counter variable in a For statement can be declared inside the For statement rather than externally, as a local variable. For example, the code:

Dim i as Integer
For i=0 to 10
 //your code goes here
Next

can be rewritten as:

For i as Integer = 0 to 10
 //your code goes here
Next

In the second instance, the counter variable goes out of scope after the last iteration of the For loop. If you wanted to read the value of the counter after the end of the loop, you couldn't because it would already be deleted from memory. In the first instance, the counter variable remains in scope for the entire method in which it is declared. You could get or set the value of the counter variable anywhere in the method.

If start, end, or step is an expression that must be evaluated, the For loop does the evaluation each time the loop is executed, even if the expression evaluates to the same value for every iteration. Therefore, the loop:

For i=0 to FontCount-1
.
.
Next

will run more slowly than coding:

Dim nFonts as Integer
nFonts= FontCount-1
For i=0 to nFonts
.
.
Next

The size of the difference, of course, depends on the speed of the computer and the time required to evaluate the expression.

You can either increment or decrement the counter. If step is positive or zero, the loop executes until counter is greater than end.

To decrement the counter, use the second syntax using DownTo and a positive value of step.

Counter is incremented or decremented when the Next statement is reached.

Counter must be a local variable and must be an Integer.

The Exit statement can be used to force the loop to stop prematurely. The Exit statement causes control to resume at the statement that follows Next.

For...Next statements can be nested to any level. However, each For...Next statement must have a different counter.

When a For loop runs, it takes over the interface, preventing the user from interacting with menus and controls. Ordinarily, this is of no concern. If, however, the loop is very lengthy, you can move the code for the loop to a separate Thread, allowing it to execute in the background.


Examples

This example uses the For...Next statement to test the values in an array and change those that are "Today" to "Tomorrow".

Dim i,last As Integer
last= Ubound(aDays)
For i=1 to last
  If aDays(i)="Today" Then
    aDays(i)="Tomorrow"
  End If
Next

The following example uses the DownTo keyword to decrement the counter:

Dim i as Integer
For i=5 DownTo 1 Step 1
  Beep
Next

See Also

Continue, Do...Loop, Exit, For...Each, While...Wend statements.